package app.tuwien.isi.tweetanalyser;

import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class TweetAdapter extends BaseAdapter {
    private final List<Tweet> tweets;
    private LayoutInflater tweetInflater;
    
    public TweetAdapter(Context context, List<Tweet> tweets) {
        this.tweets = tweets;

        tweetInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    
    public int getCount() {
        return tweets.size();
    }
    public Object getItem(int position) {
        return tweets.get(position);
    }
    public long getItemId(int position) {
        return tweets.get(position).getId();
    }
    public View getView(int position, View convertView, ViewGroup parent) {
        Tweet tweet = (Tweet) getItem(position);	
        
    	ViewHolder holder;
        
        if(convertView == null) {
        	convertView = tweetInflater.inflate(R.layout.tweet, parent, false);
        	
        	holder = new ViewHolder(); 
        	holder.textTweet = (TextView) convertView.findViewById(R.id.tweetText); 
   		 	holder.textUser = (TextView) convertView.findViewById(R.id.user); 
   		 
   		 	convertView.setTag(holder); 
        } else {
        	holder = (ViewHolder) convertView.getTag(); 
        }
        
        holder.textTweet.setText(tweet.getText()); 
        holder.textUser.setText(tweet.getUserName());
        
        try{
        	Drawable img = tweet.getImage();
            img.setBounds( 0, 0, 60, 60 );
            holder.textUser.setCompoundDrawables( img, null, null, null );
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        
        return convertView;
    }
    
    public static class ViewHolder {
        public TextView textTweet;
        public TextView textUser;
    }
}

